﻿using BBICMS.Application.ViewModels;
using BBICMS.Infrastructure.Configs;
using BBICMS.Model.Security;
using BBICMS.Repository;
using BBICMS.ViewModel;
using BBICMS.ViewModel.Admin;
using BBICMS.WebUI.Filters;
using System.Linq;
using System.Web.Mvc;


namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    [SIGAuth]
    public class RoleController : BaseController
    {
        

        // GET: Role
        public ActionResult Index()
        {
            var roles = _unit.Roles.FindBy(r => r.Id != SettingsManager.Role.Founder);
            return View(roles);
        }

        //public PartialViewResult List()
        //{
        //   // var roles = _unit.Roles.FindBy(r=>r.Id!=SettingsManager.Role.Founder);
        //    return PartialView("_List",roles);
        //}

        

        [HttpGet]
        // GET: Roles/Create
        public ActionResult SetRoleMenus(int id)
        {
            Role vRole = _unit.Roles.GetById(id);
            
            SetRoleMenusVM vm = new SetRoleMenusVM
            {
                RoleId = id,                
                Menus = _unit.Menus.GetMenusByCategoryId(SettingsManager.Menu.BackMenuCId),
                MenuIds = vRole.Menus.Select(m => m.Id).ToArray()
            };
              
            return PartialView("_SetRoleMenus", vm);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SetRoleMenus(int RoleId, int[] menuId)
        {
          
            if (RoleId > 0)
            {
              
               // _RoleService.SetRoleMenus(RoleId, menuId);

                Role vRole = _unit.Roles.GetFirstOrDefault((r=>r.Id==RoleId),(r=>r.Menus));
                vRole.Menus.Clear();
                vRole.Menus = _unit.Menus.GetAll().Where(m => menuId.Contains(m.Id)).ToList();

                _unit.Roles.Update(vRole);
                _unit.SaveChanges();
                AR.SetSuccess("编辑角色权限成功");
                return Json(AR, JsonRequestBehavior.DenyGet);
                 
            }
            AR.Setfailure("编辑角色权限失败");
            return Json(AR, JsonRequestBehavior.DenyGet);



        }

        [HttpGet]   
        public ActionResult EditRole(int? id)
        {
            Role role = (id>0)? _unit.Roles.GetById(id): new Role();
            return PartialView("_EditRole", role);
        }

  
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditRole([Bind(Include = "Id,RoleName,Description")] Role role)
        {
           

            if (ModelState.IsValid)
            {
                if (role.Id > 0)
                {
                    Role vRole = _unit.Roles.GetById(role.Id);
                    if (!vRole.IsSys)
                    {
                        vRole.RoleName = role.RoleName;
                        vRole.Description = role.Description;
                        _unit.Roles.Update(vRole);
                        _unit.SaveChanges();
                    }
                    else
                    {
                        //  return new HttpStatusCodeResult(500, "系统角色不可编辑");
                        AR.SetWarning("系统角色不可编辑");
                        return Json(AR, JsonRequestBehavior.DenyGet);
                    }
                    
                }
                else
                {
                    _unit.Roles.Add(role);
                    _unit.SaveChanges();
                }

                return RedirectToAction("List");              
            }
            else
            {
                //return new HttpStatusCodeResult(500, "编辑角色失败");

                AR.Setfailure("编辑角色失败");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            
        }



        // POST: Roles/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {

            Role role = _unit.Roles.GetById(id);
            if(role.IsSys)
            {        
                AR.SetWarning("系统角色，不可以删除！");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            if (role == null)
            {                
                AR.Setfailure("未找到此角色！");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            _unit.Roles.Delete(role);
            _unit.SaveChanges();
            return Json(AR, JsonRequestBehavior.DenyGet);
           
        }

        protected override void Dispose(bool disposing)
        {
            this._unit.Dispose();
            base.Dispose(disposing);
        }


    }
}